(function () {
    /**
     * 抛物线实现
     */
    function now() {
        return +new Date();
    }

    function Parabola() {
        this.el = null;
        this.taregetEL = null;
        this.elPosition = [0, 0];
        this.targetPosition = [0, 0];
        this.begin = null;
        this.end = null;
        this.timerId = null;
        this.duration = 1000;
        this.callback = null;
        this.driftX = 0;
        this.driftY = 0;
        this.curvature = 0.004;
        var _self = this;
        this.setConfig = function(opts){
            for(var k in opts){
                this[k] = opts[k];
            }
        };
        this.setPositions = function(elPosition, targetPosition){
            this.elPosition = elPosition;
            this.targetPosition = targetPosition;
            this.driftX = this.targetPosition[0] - this.elPosition[0];
            this.driftY = this.targetPosition[1] - this.elPosition[1];
            this.b = ( this.driftY - this.curvature * this.driftX * this.driftX ) / this.driftX;
        };
        /**
         * 定位
         * @param {Number} x x坐标 .
         * @param {Object} y y坐标 .
         * @return {Object} this
         */
        this.domove = function (x, y) {
            this.el.css({left: this.elPosition[0] + x,top: this.elPosition[1] + y});
            return this;
        };
        /**
         * 每一步执行
         * @param {Date} now 当前时间 .
         * @return {Object} this
         */
        this.step = function (now) {
            var x, y;
            if (now > this.end) {
                x = this.driftX;
                y = this.driftY;
                this.domove(x, y);
                this.stop();
                if (typeof this.callback === 'function') {
                    this.callback();
                }
            } else {
                x = this.driftX * ((now - this.begin) / this.duration);
                y = this.curvature * x * x + this.b * x;
                this.domove(x, y);
            }
            return this;
        };
        /**
         * 开始
         */
        this.start = function () {            
            // 设置起止时间
            this.begin = now();
            this.end = this.begin + this.duration;
            if (this.driftX === 0 && this.driftY === 0) {
                return;
            }
            if (!!this.timerId) {
                clearInterval(this.timerId);
                this.stop();
            }
            this.timerId = setInterval(function () {
                var t = now();
                _self.step(t);
            }, 12);
            return this;
        };
        /**
         * 重置
         */
        this.reset = function (x, y) {
            this.stop();
            x = x ? x : 0;
            y = y ? y : 0;
            this.domove(x, y);
            return this;
        };
        /**
         * 停止
         */
        this.stop = function () {
            if (!!this.timerId) {
                clearInterval(this.timerId);
            }
            return this;
        };
    }
    window.Parabola = Parabola;
})();